<?php

namespace App\Http\Controllers;

use App\Activity;
use Illuminate\Http\Request;
use Intervention\Image\Facades\Image;
use Illuminate\Support\Facades\Storage;

class ActivityController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $activities = Activity::latest('created_at')->Paginate(8);
        return view('Activity.activity',compact('activities'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("Activity.activityCreate");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $rules=[
            'activity_name'=>'required',
            'activity_place'=>'required',
            'activity_date'=>'required',
            'activity_charge'=>'required',
            'enrollments'=>'required',
            'activity_picture'=>'required',
            'activity_phone'=>'required',
            'activity_intro'=>'required'
        ];
        $this->validate($request,$rules);
        $this->uploadPicture($request);
        $activityPic=$request->get('activity_name').'.'.$request->file('activity_picture')->getClientOriginalExtension();
        $activities=new Activity;
        $activities->activity_picture=$activityPic;
        $activities->activity_name=$request->get('activity_name');
        $activities->activity_place=$request->get('activity_place');
        $activities->activity_date=$request->get('activity_date');
        $activities->activity_charge=$request->get('activity_charge');
        $activities->enrollments=$request->get('enrollments');
        $activities->activity_phone=$request->get('activity_phone');
        $activities->activity_intro=$request->get('activity_intro');
        $activities->save();

        session()->flash('success','活动创建成功！');
        return redirect()->route('activity.index');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $activities=Activity::findOrFail($id);
        return view('Activity.activityEdit',compact('activities'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $activities=Activity::findOrFail($id);
        if($request->file('activity_picture')){
            $Picture=$activities->activity_picture;
            if($Picture!='default.jpg'){
                Storage::disk('uploads')->exists($Picture);
                Storage::disk('uploads')->delete($Picture);
            }
            $this->uploadPicture($request);
            $activities->activity_picture=$request->get('activity_name'). '.' . $request->file('activity_picture')->getClientOriginalExtension();
            $activities->save();
        }
        $activities->update([
            'activity_name'=>$request->get('activity_name'),
            'activity_place'=>$request->get('activity_place'),
            'activity_date'=>$request->get('activity_date'),
            'activity_charge'=>$request->get('activity_charge'),
            'enrollments'=>$request->get('enrollments'),
            'activity_phone'=>$request->get('activity_phone'),
            'activity_intro'=>$request->get('activity_intro')
        ]);
        $activities->save();
        session()->flash('success','更新活动成功');
        return redirect()->route('activity.index');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $activity = Activity::findOrFail($id);
        $Picture=$activity->activity_picture;
        if($Picture!='default.jpg'){
            Storage::disk('uploads')->exists($Picture);
            Storage::disk('uploads')->exists($Picture);
        }
        $activity->delete();
        session()->flash('success', '删除活动成功');
        return redirect()->route('activity.index');
    }

    public function uploadPicture(Request $request){
        if($request->isMethod('post') OR $request->isMethod('patch')){
            $Picture=$request->file('activity_picture');
            if($Picture->isValid()){
                $realPath=$Picture->getRealPath();
                $ext=$Picture->getClientOriginalExtension();
                $activityName=$request->get('activity_name');
                $fileName=$request->get('activity_name').'.'.$ext;
                Image::make($Picture)->fit(300,300)->save(public_path('/uploads/avatars').$fileName);
            }
        }
    }
}
